package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.dic.bean.DicParam;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.sys.utils.NavigationBuildUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * NavigationService.
 *
 * @author zj.
 *         Created on 2019/5/9 0009.
 */
public class NavigationService extends CObject {

    /**
     * 导航设置：获取配置.
     */
    public String navigationGet (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String type = JsonTool.getStrValue(p, "type", "default");
        DicParamEnum paramEnum = DicParamEnum.NAVIGATION_KEY;
        if ("talos".equals(type)) {
            paramEnum = DicParamEnum.NAVIGATION_KEY_TALOS;
        }
        String navigationInfo = DicParamUtils.getParamValue(paramEnum.getVal());
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, navigationInfo);
    }
    /**
     * 导航设置：同步导航到所有代理.
     */
    public String navigationSyncAllAgent(HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String type = JsonTool.getStrValue(p, "type", "default");

        if (!this.sync(type)){
            return XmlCodeParser.formatResultTips(false, this.getErrCode());
        }
        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 导航设置：保存配置.
     */
    public String navigationSave (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String navigation = JsonTool.getValue(p, "navigation").toString();
        String type = JsonTool.getStrValue(p, "type", "default");
        if (StrTool.isBlankStr(navigation)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        if (!this.save(type, navigation)) {
            return XmlCodeParser.formatResultTips(false, this.getErrCode(), this.getMsg());
        }
        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 导航设置：生成.
     */
    public String navigationBuild (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        String navigation = JsonTool.getValue(p, "navigation").toString();
        String type = JsonTool.getStrValue(p, "type", "default");
        if (StrTool.isBlankStr(navigation)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        //保存
        if (!this.save(type, navigation)) {
            return XmlCodeParser.formatResultTips(false, this.getErrCode(), this.getMsg());
        }

        String rootDir = req.getSession().getServletContext().getRealPath("/");

        //生成
        NavigationBuildUtils buildUtils = new NavigationBuildUtils();
        Boolean flag = buildUtils.build(navigation, rootDir);
        if (!flag) {
            return XmlCodeParser.formatResultTips(false, buildUtils.getErrCode(), buildUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 保存配置.
     * @param type 模板类型.
     * @param navigation 导航.
     * @return 保存结果.
     */
    private Boolean save(String type, String navigation) {
        DicParamUtils paramUtils = new DicParamUtils();
        Map<String, Object> update = new HashedMap();
        update.put("paramValue", navigation);
        Map<String, Object> where = new HashedMap();
        DicParamEnum paramEnum = DicParamEnum.NAVIGATION_KEY;
        if ("talos".equals(type)) {
            paramEnum = DicParamEnum.NAVIGATION_KEY_TALOS;
        }
        where.put("paramName", paramEnum.getVal());
        Integer affect = paramUtils.modDicParam(update, where);
        if (affect < 1) {
            this.setErrCode(paramUtils.getErrCode(), paramUtils.getMsg());
            return false;
        }
        return true;
    }

    private Boolean saveParam(List<DicParam> dicParamList, int paramType){
        Boolean res = true;
        DicParamUtils dicParamUtils = new DicParamUtils();
        int ret = dicParamUtils.addListDicParam(dicParamList,paramType);
        if(ret < 0) res = false;
        return res;
    }

    /**
     * 上传轮播图
     * @param req
     * @param rsp
     * @param p
     * @param sid
     * @return
     */
    public String addNavScrollBar(HttpServletRequest req, HttpServletResponse rsp, String p, String sid){
        String paramName = JsonTool.getStrValue(p, "paramName", "");
        String paramValue = JsonTool.getStrValue(p, "paramValue", "");
        Integer paramType = JsonTool.getIntValue(p, "paramType", 1);
        DicParam param = new DicParam();
        param.setParamName(paramName);
        param.setParamValue(paramValue);
        param.setParamType(paramType);
        List<DicParam> list = new LinkedList<>();
        list.add(param);
        this.saveParam(list,paramType);
        if (!this.saveParam(list,paramType)) {
            return XmlCodeParser.formatResultTips(false, this.getErrCode(), this.getMsg());
        }
        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    public String getNavScrollBar(HttpServletRequest req, HttpServletResponse rsp, String p, String sid){
        String paramName = JsonTool.getStrValue(p, "paramName", "navBarImg");
        DicParamUtils dicParamUtils = new DicParamUtils();
        return "";
    }

    private boolean sync(String type){
        DicParamEnum paramEnum = DicParamEnum.NAVIGATION_KEY;
        if ("talos".equals(type)) {
            paramEnum = DicParamEnum.NAVIGATION_KEY_TALOS;
        }
        String navigationInfo = DicParamUtils.getParamValueByAgentIdAndParamName(paramEnum.getVal(), 0);
        if (!StrTool.isBlankStr(navigationInfo)){
            DicParamUtils utils = new DicParamUtils();
            int ret = utils.uptDicParam(navigationInfo, paramEnum.getVal(), 1, false);
            this.setErrCode(utils.getErrCode());
            utils.dispose();
            if (ret < 1 ){
                return false;
            }
        }
        return true;
    }
}
